﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data;
using System.Transactions;




public partial class Main_ServiceInvoice_CreditNote : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = string.Empty;
    int intRowCounter = 1;
    #region List Variables for Credit note main table
    List<string> LstCount;
    List<string> LstInvoiceParticular;
    List<string> LstServiceTaxRate;
    List<string> LstPECessRate;
    List<string> LstHECessRate;
    List<string> LstService;
    List<string> LstServiceTaxability;
    List<string> LstOriginalAmt;
    List<string> LstPendingAmt;
    List<string> LstAddDescription;
    List<string> LstTotalAmount;
    List<string> LstHiddenTotalAmount;

    #endregion
    #region List Variables for Credit note TDS table
    List<string> LstCountCN;
    List<string> LstParticularCN;
    List<string> LstServiceCN;
    List<string> LstTDSAmtCN;
    List<string> LstTDSAmtWithoutSTCN;
    List<string> LstDicountCN;
    List<string> LstDicountAmtCN;
    List<string> LstWriteOffCN;
    List<string> LstWriteOffAmtCN;
    List<string> LstServiceTaxAmtCN;
    List<string> LstPECessAmtCN;
    List<string> LstHECessAmtCN;
    List<string> LstTotalAmtCN;
    #endregion
    #region List Variables for Credit note Sale Return table
    List<string> LstCountSR;
    List<string> LstParticularSR;
    List<string> LstServiceSR;
    List<string> LstSaleReturnSR;
    List<string> LstSaleReturnAmtSR;
    List<string> LstTotalAmtSR;
    #endregion


    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        _Override_AddMoreRow_MainTable();
        _Override_tblCreditNote();
        _Override_tblTDSSaleReturn();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            //*******Credit Note Main Table
            LstCount = new List<string>();
            ViewState["LstCount"] = LstCount;

            LstInvoiceParticular = new List<string>();
            ViewState["LstInvoiceParticular"] = LstInvoiceParticular;

            LstServiceTaxRate = new List<string>();
            ViewState["LstServiceTaxRate"] = LstServiceTaxRate;

            LstPECessRate = new List<string>();
            ViewState["LstPECessRate"] = LstPECessRate;

            LstHECessRate = new List<string>();
            ViewState["LstHECessRate"] = LstHECessRate;

            LstService = new List<string>();
            ViewState["LstService"] = LstService;

            LstServiceTaxability = new List<string>();
            ViewState["LstServiceTaxability"] = LstServiceTaxability;

            LstOriginalAmt = new List<string>();
            ViewState["LstOriginalAmt"] = LstOriginalAmt;

            LstPendingAmt = new List<string>();
            ViewState["LstPendingAmt"] = LstPendingAmt;

            LstAddDescription = new List<string>();
            ViewState["LstAddDescription"] = LstAddDescription;

            LstTotalAmount = new List<string>();
            ViewState["LstTotalAmount"] = LstTotalAmount;

            LstHiddenTotalAmount = new List<string>();
            ViewState["LstHiddenTotalAmount"] = LstHiddenTotalAmount;
            //**********Credit Note TDS Table
            LstCountCN = new List<string>();
            ViewState["LstCountCN"] = LstCountCN;

            LstParticularCN = new List<string>();
            ViewState["LstParticularCN"] = LstParticularCN;

            LstServiceCN = new List<string>();
            ViewState["LstServiceCN"] = LstServiceCN;

            LstTDSAmtCN = new List<string>();
            ViewState["LstTDSAmtCN"] = LstTDSAmtCN;

            LstTDSAmtWithoutSTCN = new List<string>();
            ViewState["LstTDSAmtWithoutSTCN"] = LstTDSAmtWithoutSTCN;

            LstDicountCN = new List<string>();
            ViewState["LstDicountCN"] = LstDicountCN;

            LstDicountAmtCN = new List<string>();
            ViewState["LstDicountAmtCN"] = LstDicountAmtCN;

            LstWriteOffCN = new List<string>();
            ViewState["LstWriteOffCN"] = LstWriteOffCN;

            LstWriteOffAmtCN = new List<string>();
            ViewState["LstWriteOffAmtCN"] = LstWriteOffAmtCN;

            LstServiceTaxAmtCN = new List<string>();
            ViewState["LstServiceTaxAmtCN"] = LstServiceTaxAmtCN;

            LstPECessAmtCN = new List<string>();
            ViewState["LstPECessAmtCN"] = LstPECessAmtCN;

            LstHECessAmtCN = new List<string>();
            ViewState["LstHECessAmtCN"] = LstHECessAmtCN;

            LstTotalAmtCN = new List<string>();
            ViewState["LstTotalAmtCN"] = LstTotalAmtCN;
            
            //**********Sale Return Table******

            LstCountSR = new List<string>();
            ViewState["LstCountSR"] = LstCountSR;

            LstParticularSR = new List<string>();
            ViewState["LstParticularSR"] = LstParticularSR;

            LstServiceSR = new List<string>();
            ViewState["LstServiceSR"] = LstServiceSR;

            LstSaleReturnSR = new List<string>();
            ViewState["LstSaleReturnSR"] = LstSaleReturnSR;

            LstSaleReturnAmtSR = new List<string>();
            ViewState["LstSaleReturnAmtSR"] = LstSaleReturnAmtSR;

            LstTotalAmtSR = new List<string>();
            ViewState["LstTotalAmtSR"] = LstTotalAmtSR;

            _GetCurrentServiceTaxRate(txtDate.Text);
            _GetParticular_MainTable(ddlInvoiceParticular1);
            _EnableDisableControls(ddlFormat.SelectedValue);
            _BindTransactionFormat();
           _BindCustomerLedger();
            //txtDate.Text = string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(DateTime.Now.ToString()));
            _GetCurrentServiceTaxRate(txtDate.Text.ToString());

        }
    }
    private void _BindTransactionFormat()
    {
        SqlParameter[] sparam ={
                                   new SqlParameter("@TRANSACTIONTYPE","4")
                               };
        DataTable DTFormat = DU.getdatausingprocedure("USP_IVC_GetAllTransactionFormatNameID", sparam);
        if (DTFormat.Rows.Count > 0)
        {
            ddlFormat.DataSource = DTFormat;
            ddlFormat.DataTextField = "FORMATNAME";
            ddlFormat.DataValueField = "FORMATID";
            ddlFormat.DataBind();
            ddlFormat.Items.Insert(0, new ListItem("-Select Format-", "0"));
        }
    }
    private void _BindCustomerLedger()
    {
        DataTable DTFormat = DU.getdatausingprocedure("USP_IVC_GetCustomerLedger");
        if (DTFormat.Rows.Count > 0)
        {
            ddlCustomer.DataSource = DTFormat;
            ddlCustomer.DataTextField = "LNAME";
            ddlCustomer.DataValueField = "LID";
            ddlCustomer.DataBind();
            ddlCustomer.Items.Insert(0, new ListItem("-Select Customer-", "0"));
        }
    }
    private void _GetCurrentServiceTaxRate(string strCurrentDate)
    {
        if (strCurrentDate != string.Empty)
        {
            string[] strTemp = strCurrentDate.ToString().Split('/');
            string strDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];

            SqlParameter[] sparam ={
                                       new SqlParameter("@CURRENTDATE",strDate)
                                   };
            DataTable DTCurrentST = DU.getdatausingprocedure("USP_IVC_GetCurrentServiceTaxRate", sparam);
            if (DTCurrentST.Rows.Count > 0)
            {
                txtServiceTaxPer.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["ServiceTax"].ToString())).ToString();
                txtPECessPer.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["PCess"].ToString())).ToString();
                txtHECessPer.Text = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["HCess"].ToString())).ToString();

                hidServiceTaxRateCurrent.Value = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["SERVICETAX"].ToString()));
                hidPECessRateCurrent.Value = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["PCess"].ToString()));
                hidHECessRateCurrent.Value = string.Format("{0:0.00}", Convert.ToDouble(DTCurrentST.Rows[0]["HCess"].ToString()));

                hidServiceTaxLid.Value = DTCurrentST.Rows[0]["SERVICETAX_LID"].ToString();
                hidPECessLid.Value = DTCurrentST.Rows[0]["PCess_LID"].ToString();
                hidHECessLid.Value = DTCurrentST.Rows[0]["HCess_LID"].ToString();
            }
        }
    }
    private void _AddMoreRow_MainTable()
    {
        intRowCounter++;
        HtmlTableRow HTRow = new HtmlTableRow();

        HtmlTableCell HTCell1 = new HtmlTableCell();
        DropDownList ddlInvoiceParticular = new DropDownList();
        ddlInvoiceParticular.ID = "ddlInvoiceParticular" + intRowCounter.ToString();
        ddlInvoiceParticular.CssClass = "dropdown";
        ddlInvoiceParticular.Width = Unit.Percentage(98);
        _GetParticular_MainTable(ddlInvoiceParticular);
        ddlInvoiceParticular.AutoPostBack = true;
        ddlInvoiceParticular.SelectedIndexChanged += new EventHandler(ddlParticular_OnSelectedIndexChanged);
        HiddenField hidServiceTaxRate = new HiddenField();
        hidServiceTaxRate.ID = "hidServiceTaxRate" + intRowCounter.ToString();
        HiddenField hidPECessRate = new HiddenField();
        hidPECessRate.ID = "hidPECessRate" + intRowCounter.ToString();
        HiddenField hidHECessRate = new HiddenField();
        hidHECessRate.ID = "hidHECessRate" + intRowCounter.ToString();

        HTCell1.Controls.Add(ddlInvoiceParticular);
        HTCell1.Controls.Add(hidServiceTaxRate);
        HTCell1.Controls.Add(hidPECessRate);
        HTCell1.Controls.Add(hidHECessRate);

        LstCount.Add(intRowCounter.ToString());
        LstInvoiceParticular.Add(ddlInvoiceParticular.ID);
        LstServiceTaxRate.Add(hidServiceTaxRate.ID);
        LstPECessRate.Add(hidPECessRate.ID);
        LstHECessRate.Add(hidHECessRate.ID);

        HtmlTableCell HTCell2 = new HtmlTableCell();
        DropDownList ddlServices = new DropDownList();
        ddlServices.ID = "ddlServices" + intRowCounter.ToString();
        ddlServices.CssClass = "dropdown";
        ddlServices.Width = Unit.Percentage(98);
        ddlServices.Attributes.Add("onchange", "return NoServiceRepeat(this.id);");
        ddlServices.AutoPostBack = true;
        ddlServices.SelectedIndexChanged += new EventHandler(ddlServiecs_OnSelectedIndexChanged);

        HiddenField hidServiceTaxability = new HiddenField();
        hidServiceTaxability.ID = "hidServiceTaxability" + intRowCounter.ToString();
        HTCell2.Controls.Add(ddlServices);
        HTCell2.Controls.Add(hidServiceTaxability);
        LstService.Add(ddlServices.ID);
        LstServiceTaxability.Add(hidServiceTaxability.ID);

        HtmlTableCell HTCell3 = new HtmlTableCell();
        TextBox txtOriginalAmt = new TextBox();
        txtOriginalAmt.ID = "txtOriginalAmt" + intRowCounter.ToString();
        txtOriginalAmt.CssClass = "DisableInputNumber";
        txtOriginalAmt.Width = Unit.Percentage(98);
        txtOriginalAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
        HTCell3.Controls.Add(txtOriginalAmt);
        LstOriginalAmt.Add(txtOriginalAmt.ID);

        HtmlTableCell HTCell4 = new HtmlTableCell();
        TextBox txtPendingAmt = new TextBox();
        txtPendingAmt.ID = "txtPendingAmt" + intRowCounter.ToString();
        txtPendingAmt.CssClass = "DisableInputNumber";
        txtPendingAmt.Width = Unit.Percentage(98);
        txtPendingAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
        HTCell4.Controls.Add(txtPendingAmt);
        LstPendingAmt.Add(txtPendingAmt.ID);

        HtmlTableCell HTCell5 = new HtmlTableCell();
        TextBox txtAddDescription = new TextBox();
        txtAddDescription.ID = "txtAddDescription" + intRowCounter.ToString();
        txtAddDescription.CssClass = "input";
        txtAddDescription.Width = Unit.Percentage(98);
        HTCell5.Controls.Add(txtAddDescription);
        LstAddDescription.Add(txtAddDescription.ID);

        HtmlTableCell HTCell6 = new HtmlTableCell();
        TextBox txtTotalAmount = new TextBox();
        txtTotalAmount.ID = "txtTotalAmount" + intRowCounter.ToString();
        txtTotalAmount.CssClass = "input";
        txtTotalAmount.Width = Unit.Percentage(98);
        txtTotalAmount.Attributes.Add("onkeypress", "return isNumberKey(this.id, event);");
        txtTotalAmount.Attributes.Add("onblur", "return SetAmount(this.id);");
        HiddenField hidTotalAmout = new HiddenField();
        hidTotalAmout.ID = "hidTotalAmout" + intRowCounter.ToString();

        HTCell6.Controls.Add(txtTotalAmount);
        HTCell6.Controls.Add(hidTotalAmout);
        LstTotalAmount.Add(txtTotalAmount.ID);
        LstHiddenTotalAmount.Add(hidTotalAmout.ID);



        HTRow.Cells.Add(HTCell1);
        HTRow.Cells.Add(HTCell2);
        HTRow.Cells.Add(HTCell3);
        HTRow.Cells.Add(HTCell4);
        HTRow.Cells.Add(HTCell5);
        HTRow.Cells.Add(HTCell6);

        tblCreditNoteMain.Rows.Add(HTRow);


        ViewState["LstCount"] = LstCount;
        ViewState["LstInvoiceParticular"] = LstInvoiceParticular;
        ViewState["LstServiceTaxRate"] = LstServiceTaxRate;
        ViewState["LstPECessRate"] = LstPECessRate;
        ViewState["LstHECessRate"] = LstHECessRate;
        ViewState["LstService"] = LstService;
        ViewState["LstServiceTaxability"] = LstServiceTaxability;
        ViewState["LstOriginalAmt"] = LstOriginalAmt;
        ViewState["LstPendingAmt"] = LstPendingAmt;
        ViewState["LstAddDescription"] = LstAddDescription;
        ViewState["LstTotalAmount"] = LstTotalAmount;
        ViewState["LstHiddenTotalAmount"] = LstHiddenTotalAmount;

        hidRowCountMain.Value = intRowCounter.ToString();

    }

    private void _Override_AddMoreRow_MainTable()
    {
        LstCount = (List<string>)ViewState["LstCount"];
        LstInvoiceParticular = (List<string>)ViewState["LstInvoiceParticular"];
        LstServiceTaxRate = (List<string>)ViewState["LstServiceTaxRate"];
        LstPECessRate = (List<string>)ViewState["LstPECessRate"];
        LstHECessRate = (List<string>)ViewState["LstHECessRate"];
        LstService = (List<string>)ViewState["LstService"];
        LstServiceTaxability = (List<string>)ViewState["LstServiceTaxability"];
        LstOriginalAmt = (List<string>)ViewState["LstOriginalAmt"];
        LstPendingAmt = (List<string>)ViewState["LstPendingAmt"];
        LstAddDescription = (List<string>)ViewState["LstAddDescription"];
        LstTotalAmount = (List<string>)ViewState["LstTotalAmount"];
        LstHiddenTotalAmount = (List<string>)ViewState["LstHiddenTotalAmount"];
        if (LstCount != null)
        {
            int intCount = Convert.ToInt32(LstCount.Count);
            for (int i = 0; i < intCount; i++)
            {
                intRowCounter++;
                HtmlTableRow HTRow = new HtmlTableRow();

                HtmlTableCell HTCell1 = new HtmlTableCell();
                DropDownList ddlInvoiceParticular = new DropDownList();
                ddlInvoiceParticular.ID = LstInvoiceParticular[i].ToString();
                ddlInvoiceParticular.CssClass = "dropdown";
                ddlInvoiceParticular.Width = Unit.Percentage(98);
                _GetParticular_MainTable(ddlInvoiceParticular);
                ddlInvoiceParticular.AutoPostBack = true;
                ddlInvoiceParticular.SelectedIndexChanged += new EventHandler(ddlParticular_OnSelectedIndexChanged);
                HiddenField hidServiceTaxRate = new HiddenField();
                hidServiceTaxRate.ID = LstServiceTaxRate[i].ToString();
                HiddenField hidPECessRate = new HiddenField();
                hidPECessRate.ID = LstPECessRate[i].ToString();
                HiddenField hidHECessRate = new HiddenField();
                hidHECessRate.ID = LstHECessRate[i].ToString();

                HTCell1.Controls.Add(ddlInvoiceParticular);
                HTCell1.Controls.Add(hidServiceTaxRate);
                HTCell1.Controls.Add(hidPECessRate);
                HTCell1.Controls.Add(hidHECessRate);

                HtmlTableCell HTCell2 = new HtmlTableCell();
                DropDownList ddlServices = new DropDownList();
                ddlServices.ID = LstService[i].ToString();
                ddlServices.CssClass = "dropdown";
                ddlServices.Width = Unit.Percentage(98);
                ddlServices.AutoPostBack = true;
                ddlServices.Attributes.Add("onchange", "return NoServiceRepeat(this.id);");
                ddlServices.SelectedIndexChanged += new EventHandler(ddlServiecs_OnSelectedIndexChanged);

                HiddenField hidServiceTaxability = new HiddenField();
                hidServiceTaxability.ID = LstServiceTaxability[i].ToString();
                HTCell2.Controls.Add(ddlServices);
                HTCell2.Controls.Add(hidServiceTaxability);



                HtmlTableCell HTCell3 = new HtmlTableCell();
                TextBox txtOriginalAmt = new TextBox();
                txtOriginalAmt.ID = LstOriginalAmt[i].ToString();
                txtOriginalAmt.CssClass = "DisableInputNumber";
                txtOriginalAmt.Width = Unit.Percentage(98);
                txtOriginalAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HTCell3.Controls.Add(txtOriginalAmt);

                HtmlTableCell HTCell4 = new HtmlTableCell();
                TextBox txtPendingAmt = new TextBox();
                txtPendingAmt.ID = LstPendingAmt[i].ToString();
                txtPendingAmt.CssClass = "DisableInputNumber";
                txtPendingAmt.Width = Unit.Percentage(98);
                txtPendingAmt.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HTCell4.Controls.Add(txtPendingAmt);


                HtmlTableCell HTCell5 = new HtmlTableCell();
                TextBox txtAddDescription = new TextBox();
                txtAddDescription.ID = LstAddDescription[i].ToString();
                txtAddDescription.CssClass = "input";
                txtAddDescription.Width = Unit.Percentage(98);
                HTCell5.Controls.Add(txtAddDescription);


                HtmlTableCell HTCell6 = new HtmlTableCell();
                TextBox txtTotalAmount = new TextBox();
                txtTotalAmount.ID = LstTotalAmount[i].ToString();
                txtTotalAmount.CssClass = "inputnumber";
                txtTotalAmount.Width = Unit.Percentage(98);
                txtTotalAmount.Attributes.Add("onkeypress", "return isNumberKey(this.id, event);");
                txtTotalAmount.Attributes.Add("onblur", "return SetAmount(this.id);");
                HiddenField hidTotalAmout = new HiddenField();
                hidTotalAmout.ID = LstHiddenTotalAmount[i].ToString();

                HTCell6.Controls.Add(txtTotalAmount);
                HTCell6.Controls.Add(hidTotalAmout);

                HTRow.Cells.Add(HTCell1);
                HTRow.Cells.Add(HTCell2);
                HTRow.Cells.Add(HTCell3);
                HTRow.Cells.Add(HTCell4);
                HTRow.Cells.Add(HTCell5);
                HTRow.Cells.Add(HTCell6);

                tblCreditNoteMain.Rows.Add(HTRow);
            }
        }
    }
    private void _DeleteAllRow_MainTable()
    {
        int rowCount = Convert.ToInt32(hidRowCountMain.Value);
        int rowIndex = rowCount;
        int tblRowCount = Convert.ToInt32(tblCreditNoteMain.Rows.Count);
        if (tblRowCount > 2)
        {
            for (int i = tblRowCount; i > 2; i--)
            {
                LstCount = (List<string>)ViewState["LstCount"];
                LstInvoiceParticular = (List<string>)ViewState["LstInvoiceParticular"];
                LstServiceTaxRate = (List<string>)ViewState["LstServiceTaxRate"];
                LstPECessRate = (List<string>)ViewState["LstPECessRate"];
                LstHECessRate = (List<string>)ViewState["LstHECessRate"];
                LstService = (List<string>)ViewState["LstService"];
                LstServiceTaxability = (List<string>)ViewState["LstServiceTaxability"];
                LstOriginalAmt = (List<string>)ViewState["LstOriginalAmt"];
                LstPendingAmt = (List<string>)ViewState["LstPendingAmt"];
                LstAddDescription = (List<string>)ViewState["LstAddDescription"];
                LstTotalAmount = (List<string>)ViewState["LstTotalAmount"];
                LstHiddenTotalAmount = (List<string>)ViewState["LstHiddenTotalAmount"];

                tblCreditNoteMain.Rows.RemoveAt(i - 1);

                LstCount.RemoveAt(i - 3);
                LstInvoiceParticular.RemoveAt(i - 3);
                LstServiceTaxRate.RemoveAt(i - 3);
                LstPECessRate.RemoveAt(i - 3);
                LstHECessRate.RemoveAt(i - 3);
                LstService.RemoveAt(i - 3);
                LstServiceTaxability.RemoveAt(i - 3);
                LstOriginalAmt.RemoveAt(i - 3);
                LstPendingAmt.RemoveAt(i - 3);
                LstAddDescription.RemoveAt(i - 3);
                LstTotalAmount.RemoveAt(i - 3);
                LstHiddenTotalAmount.RemoveAt(i - 3);

                ViewState["LstCount"] = LstCount;
                ViewState["LstInvoiceParticular"] = LstInvoiceParticular;
                ViewState["LstServiceTaxRate"] = LstServiceTaxRate;
                ViewState["LstPECessRate"] = LstPECessRate;
                ViewState["LstHECessRate"] = LstHECessRate;
                ViewState["LstService"] = LstService;
                ViewState["LstServiceTaxability"] = LstServiceTaxability;
                ViewState["LstOriginalAmt"] = LstOriginalAmt;
                ViewState["LstPendingAmt"] = LstPendingAmt;
                ViewState["LstAddDescription"] = LstAddDescription;
                ViewState["LstTotalAmount"] = LstTotalAmount;
                ViewState["LstHiddenTotalAmount"] = LstHiddenTotalAmount;

                hidRowCountMain.Value = "1";
            }
        }
    }
    private void _DeleteAllRow_Details()
    {
        if (tblTdsCreditNote.Rows.Count > 1)
        {
            for (int i = tblTdsCreditNote.Rows.Count; i > 1; i--)
            {

                LstCountCN = (List<string>)ViewState["LstCountCN"];
                LstParticularCN = (List<string>)ViewState["LstParticularCN"];
                LstServiceCN = (List<string>)ViewState["LstServiceCN"];
                LstTDSAmtCN = (List<string>)ViewState["LstTDSAmtCN"];
                LstTDSAmtWithoutSTCN = (List<string>)ViewState["LstTDSAmtWithoutSTCN"];
                LstDicountCN = (List<string>)ViewState["LstDicountCN"];
                LstDicountAmtCN = (List<string>)ViewState["LstDicountAmtCN"];
                LstWriteOffCN = (List<string>)ViewState["LstWriteOffCN"];
                LstWriteOffAmtCN = (List<string>)ViewState["LstWriteOffAmtCN"];
                LstServiceTaxAmtCN = (List<string>)ViewState["LstServiceTaxAmtCN"];
                LstPECessAmtCN = (List<string>)ViewState["LstPECessAmtCN"];
                LstHECessAmtCN = (List<string>)ViewState["LstHECessAmtCN"];
                LstTotalAmtCN = (List<string>)ViewState["LstTotalAmtCN"];

                tblTdsCreditNote.Rows.RemoveAt(i - 1);

                LstCountCN.RemoveAt(i - 2);
                LstParticularCN.RemoveAt(i - 2);
                LstServiceCN.RemoveAt(i - 2);
                LstTDSAmtCN.RemoveAt(i - 2);
                LstTDSAmtWithoutSTCN.RemoveAt(i - 2);
                LstDicountCN.RemoveAt(i - 2);
                LstDicountAmtCN.RemoveAt(i - 2);
                LstWriteOffCN.RemoveAt(i - 2);
                LstWriteOffAmtCN.RemoveAt(i - 2);
                LstServiceTaxAmtCN.RemoveAt(i - 2);
                LstPECessAmtCN.RemoveAt(i - 2);
                LstHECessAmtCN.RemoveAt(i - 2);
                LstTotalAmtCN.RemoveAt(i - 2);


                ViewState["LstCountCN"] = LstCountCN;
                ViewState["LstParticularCN"] = LstParticularCN;
                ViewState["LstServiceCN"] = LstServiceCN;
                ViewState["LstTDSAmtCN"] = LstTDSAmtCN;
                ViewState["LstTDSAmtWithoutSTCN"] = LstTDSAmtWithoutSTCN;
                ViewState["LstDicountCN"] = LstDicountCN;
                ViewState["LstDicountAmtCN"] = LstDicountAmtCN;
                ViewState["LstWriteOffCN"] = LstWriteOffCN;
                ViewState["LstWriteOffAmtCN"] = LstWriteOffAmtCN;
                ViewState["LstServiceTaxAmtCN"] = LstServiceTaxAmtCN;
                ViewState["LstPECessAmtCN"] = LstPECessAmtCN;
                ViewState["LstHECessAmtCN"] = LstHECessAmtCN;
                ViewState["LstTotalAmtCN"] = LstTotalAmtCN;

                hidRowCountCN.Value = "0";
            }
        }
        if (tblTDSSaleReturn.Rows.Count > 1)
        {
            for (int i = tblTDSSaleReturn.Rows.Count; i > 1; i--)
            {
                LstCountSR = (List<string>)ViewState["LstCountSR"];
                LstParticularSR = (List<string>)ViewState["LstParticularSR"];
                LstServiceSR = (List<string>)ViewState["LstServiceSR"];
                LstSaleReturnSR = (List<string>)ViewState["LstSaleReturnSR"];
                LstTotalAmtSR = (List<string>)ViewState["LstTotalAmtSR"]; ;

                tblTDSSaleReturn.Rows.RemoveAt(i - 1);

                LstCountSR.RemoveAt(i - 2);
                LstParticularSR.RemoveAt(i - 2);
                LstServiceSR.RemoveAt(i - 2);
                LstSaleReturnSR.RemoveAt(i - 2);
                LstTotalAmtSR.RemoveAt(i - 2);

                ViewState["LstCountSR"] = LstCountSR;
                ViewState["LstParticularSR"] = LstParticularSR;
                ViewState["LstServiceSR"] = LstServiceSR;
                ViewState["LstSaleReturnSR"] = LstSaleReturnSR;
                ViewState["LstTotalAmtSR"] = LstTotalAmtSR;

                hidRowCountSR.Value = "0";
            }
        }
    }
    private void _BindDetails_tblTDSCreditNote(int rowIndex)
    {
        DropDownList ddlInvoiceParticular = (DropDownList)tblCreditNoteMain.Rows[rowIndex].FindControl("ddlInvoiceParticular" + rowIndex);
        TextBox txtTotalAmount = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtTotalAmount" + rowIndex);
        DropDownList ddlServices = (DropDownList)tblCreditNoteMain.Rows[rowIndex].FindControl("ddlServices" + rowIndex);

        TextBox txtParticularCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtParticularCN" + rowIndex);
        TextBox txtServiceCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtServiceCN" + rowIndex);
        TextBox txtTDSAmtCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtTDSAmtCN" + rowIndex);
        TextBox txtDiscountCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtDiscountCN" + rowIndex);
        TextBox txtWriteOffCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtWriteOffCN" + rowIndex);
        TextBox txtTotalAmtCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtTotalAmtCN" + rowIndex);
        txtParticularCN.Text = ddlInvoiceParticular.SelectedItem.Text;
        txtServiceCN.Text = ddlServices.SelectedItem.Text;
        string strTaxability = _GetRembursmentService(ddlServices.SelectedValue);
        if (strTaxability == "1")
        {
            txtDiscountCN.Text = "";
            txtTDSAmtCN.Enabled = true;
            txtTDSAmtCN.Text = txtTotalAmount.Text;
            txtTDSAmtCN.CssClass = "inputnumber";
        }
        else
        {
            txtDiscountCN.Text = txtTotalAmount.Text;
            txtTDSAmtCN.Text = "";
            txtTDSAmtCN.Enabled = false;
            txtTDSAmtCN.CssClass = "DisableInputNumber";
        }
        txtTotalAmtCN.Text = txtTotalAmount.Text;
    }
    private void _BindDetails_tblTDSSaleReturn(int rowIndex)
    {
        DropDownList ddlInvoiceParticular = (DropDownList)tblCreditNoteMain.Rows[rowIndex].FindControl("ddlInvoiceParticular" + rowIndex);
        TextBox txtTotalAmount = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtTotalAmount" + rowIndex);
        DropDownList ddlServices = (DropDownList)tblCreditNoteMain.Rows[rowIndex].FindControl("ddlServices" + rowIndex);

        TextBox txtParticularSR = (TextBox)tblTDSSaleReturn.Rows[rowIndex].FindControl("txtParticularSR" + rowIndex);
        TextBox txtServiceSR = (TextBox)tblTDSSaleReturn.Rows[rowIndex].FindControl("txtServiceSR" + rowIndex);
        TextBox txtSaleReturnSR = (TextBox)tblTDSSaleReturn.Rows[rowIndex].FindControl("txtSaleReturnSR" + rowIndex);
        TextBox txtTotalAmtSR = (TextBox)tblTDSSaleReturn.Rows[rowIndex].FindControl("txtTotalAmtSR" + rowIndex);

        txtParticularSR.Text = ddlInvoiceParticular.SelectedItem.Text;
        txtServiceSR.Text = ddlServices.SelectedItem.Text;
        txtSaleReturnSR.Text = txtTotalAmount.Text;
        txtTotalAmtSR.Text = txtTotalAmount.Text;
    }
    private DataTable _ReturnParticular()
    {
        string strTranDate = string.Empty;
        DataTable DTParticular = new DataTable();
        if (txtDate.Text != string.Empty)
        {
            string[] str = txtDate.Text.Split('/');
            strTranDate = str[1] + "/" + str[0] + "/" + str[2];
        }
        if (ddlCustomer.SelectedValue != "0" && strTranDate != string.Empty)
        {
            SqlParameter[] sparam ={
                                   new SqlParameter("@LID",ddlCustomer.SelectedValue),
                                   new SqlParameter("@VOUDATE",strTranDate),
                                   new SqlParameter("@FINYEAR",Session["FINYEAR"].ToString()),
                                   new SqlParameter("@CURRENTTRANID","0"),
                                   new SqlParameter("@ISSALESRETURN",rbtCreditNoteType.SelectedValue)
                               };
            DTParticular = DU.getdatausingprocedure("USP_IVC_GetPendingParticulars", sparam);
        }
        return DTParticular;
    }
    private DataTable _ReturnPendingService_Details()
    {
        string strTranDate = string.Empty;
        DataTable DTServiceDetails = new DataTable();
        if (txtDate.Text != string.Empty)
        {
            string[] str = txtDate.Text.Split('/');
            strTranDate = str[1] + "/" + str[0] + "/" + str[2];
        }
        if (ddlCustomer.SelectedValue != "0" && strTranDate != string.Empty)
        {
            SqlParameter[] sparam ={
                                   new SqlParameter("@CUSTOMERLEDGERID",ddlCustomer.SelectedValue),
                                   new SqlParameter("@VOUDATE",strTranDate),                                   
                                   new SqlParameter("@TRANID","0"),
                                   new SqlParameter("@ISSALESRETURN",rbtCreditNoteType.SelectedValue)
                               };
            DTServiceDetails = DU.getdatausingprocedure("USP_IVC_GetAllPendingServiceDetails", sparam);
        }
        return DTServiceDetails;
    }
    private DataTable _GetBilledServiceTaxRate(string strTranId)
    {
        DataTable DTServiceTax = new DataTable();
        if (strTranId != string.Empty)
        {
            SqlParameter[] sparam ={                                  
                                   new SqlParameter("@TRAINID",strTranId)
                               };
            DTServiceTax = DU.getdatausingprocedure("USP_IVC_GetTransactionServiceRates", sparam);
        }
        return DTServiceTax;
    }
    private void _GetParticular_MainTable(DropDownList ddl)
    {
        DataTable DTParticular = _ReturnParticular();
        ddl.DataSource = DTParticular;
        ddl.DataTextField = "Reference";
        ddl.DataValueField = "ReferenceId";
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem("", "0"));
    }
    private void _AddRow_tblCreditNote(int RowCount)
    {
        if (tblTdsCreditNote.Rows.Count == RowCount)
        {
            HtmlTableRow HTRow = new HtmlTableRow();

            HtmlTableCell HTCell1 = new HtmlTableCell();
            TextBox txtParticularCN = new TextBox();
            txtParticularCN.ID = "txtParticularCN" + RowCount.ToString();
            txtParticularCN.CssClass = "DisableInput";
            txtParticularCN.Width = Unit.Percentage(98);
            txtParticularCN.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
            HTCell1.Controls.Add(txtParticularCN);
            LstCountCN.Add(RowCount.ToString());
            LstParticularCN.Add(txtParticularCN.ID);

            HtmlTableCell HTCell2 = new HtmlTableCell();
            TextBox txtServiceCN = new TextBox();
            txtServiceCN.ID = "txtServiceCN" + RowCount.ToString();
            txtServiceCN.CssClass = "DisableInput";
            txtServiceCN.Width = Unit.Percentage(98);
            txtServiceCN.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
            HTCell2.Controls.Add(txtServiceCN);
            LstServiceCN.Add(txtServiceCN.ID);

            HtmlTableCell HTCell3 = new HtmlTableCell();
            TextBox txtTDSAmtCN = new TextBox();
            txtTDSAmtCN.ID = "txtTDSAmtCN" + RowCount.ToString();
            txtTDSAmtCN.CssClass = "inputnumber";
            txtTDSAmtCN.Width = Unit.Percentage(98);
            txtTDSAmtCN.Attributes.Add("onkeypress", "return isNumberKey(this.id, event);");
            txtTDSAmtCN.Attributes.Add("onblur", "return CalculateTdsCreditNoteAmt(this.id);");
            HiddenField hidTDSAmtWithoutSTCN = new HiddenField();
            hidTDSAmtWithoutSTCN.ID = "hidTDSAmtWithoutSTCN" + RowCount.ToString();
            HiddenField hidServiceTaxAmtCN = new HiddenField();
            hidServiceTaxAmtCN.ID = "hidServiceTaxAmtCN" + RowCount.ToString();
            HiddenField hidPECessAmtCN = new HiddenField();
            hidPECessAmtCN.ID = "hidPECessAmtCN" + RowCount.ToString();
            HiddenField hidHECessAmtCN = new HiddenField();
            hidHECessAmtCN.ID = "hidHECessAmtCN" + RowCount.ToString();
            HTCell3.Controls.Add(txtTDSAmtCN);
            HTCell3.Controls.Add(hidTDSAmtWithoutSTCN);
            HTCell3.Controls.Add(hidServiceTaxAmtCN);
            HTCell3.Controls.Add(hidPECessAmtCN);
            HTCell3.Controls.Add(hidHECessAmtCN);
            LstTDSAmtCN.Add(txtTDSAmtCN.ID);
            LstTDSAmtWithoutSTCN.Add(hidTDSAmtWithoutSTCN.ID);
            LstServiceTaxAmtCN.Add(hidServiceTaxAmtCN.ID);
            LstPECessAmtCN.Add(hidPECessAmtCN.ID);
            LstHECessAmtCN.Add(hidHECessAmtCN.ID);

            HtmlTableCell HTCell4 = new HtmlTableCell();
            TextBox txtDiscountCN = new TextBox();
            txtDiscountCN.ID = "txtDiscountCN" + RowCount.ToString();
            txtDiscountCN.CssClass = "inputnumber";
            txtDiscountCN.Width = Unit.Percentage(98);
            txtDiscountCN.Attributes.Add("onkeypress", "return isNumberKey(this.id, event);");
            txtDiscountCN.Attributes.Add("onblur", "return CalculateTdsCreditNoteAmt(this.id);");
            HiddenField hidDiscountAmtCN = new HiddenField();
            hidDiscountAmtCN.ID = "hidDiscountAmtCN" + RowCount.ToString();
            HTCell4.Controls.Add(txtDiscountCN);
            HTCell4.Controls.Add(hidDiscountAmtCN);
            LstDicountCN.Add(txtDiscountCN.ID);
            LstDicountAmtCN.Add(hidDiscountAmtCN.ID);

            HtmlTableCell HTCell5 = new HtmlTableCell();
            TextBox txtWriteOffCN = new TextBox();
            txtWriteOffCN.ID = "txtWriteOffCN" + RowCount.ToString();
            txtWriteOffCN.CssClass = "inputnumber";
            txtWriteOffCN.Width = Unit.Percentage(98);
            txtWriteOffCN.Attributes.Add("onkeypress", "return isNumberKey(this.id, event);");
            txtWriteOffCN.Attributes.Add("onblur", "return CalculateTdsCreditNoteAmt(this.id);");
            HiddenField hidWriteOffAmtCN = new HiddenField();
            hidWriteOffAmtCN.ID = "hidWriteOffAmtCN" + RowCount.ToString();
            HTCell5.Controls.Add(txtWriteOffCN);
            HTCell5.Controls.Add(hidWriteOffAmtCN);
            LstWriteOffCN.Add(txtWriteOffCN.ID);
            LstWriteOffAmtCN.Add(hidWriteOffAmtCN.ID);

            HtmlTableCell HTCell6 = new HtmlTableCell();
            TextBox txtTotalAmtCN = new TextBox();
            txtTotalAmtCN.ID = "txtTotalAmtCN" + RowCount.ToString();
            txtTotalAmtCN.CssClass = "DisableInputNumber";
            txtTotalAmtCN.Width = Unit.Percentage(98);
            txtTotalAmtCN.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
            HTCell6.Controls.Add(txtTotalAmtCN);
            LstTotalAmtCN.Add(txtTotalAmtCN.ID);

            HTRow.Cells.Add(HTCell1);
            HTRow.Cells.Add(HTCell2);
            HTRow.Cells.Add(HTCell3);
            HTRow.Cells.Add(HTCell4);
            HTRow.Cells.Add(HTCell5);
            HTRow.Cells.Add(HTCell6);
            tblTdsCreditNote.Rows.Add(HTRow);

            hidRowCountCN.Value = RowCount.ToString();

            ViewState["LstCountCN"] = LstCountCN;
            ViewState["LstParticularCN"] = LstParticularCN;
            ViewState["LstServiceCN"] = LstServiceCN;
            ViewState["LstTDSAmtCN"] = LstTDSAmtCN;
            ViewState["LstTDSAmtWithoutSTCN"] = LstTDSAmtWithoutSTCN;
            ViewState["LstDicountCN"] = LstDicountCN;
            ViewState["LstDicountAmtCN"] = LstDicountAmtCN;
            ViewState["LstWriteOffCN"] = LstWriteOffCN;
            ViewState["LstWriteOffAmtCN"] = LstWriteOffAmtCN;
            ViewState["LstServiceTaxAmtCN"] = LstServiceTaxAmtCN;
            ViewState["LstPECessAmtCN"] = LstPECessAmtCN;
            ViewState["LstHECessAmtCN"] = LstHECessAmtCN;
            ViewState["LstTotalAmtCN"] = LstTotalAmtCN;
        }
    }
    private void _Override_tblCreditNote()
    {
        LstCountCN = (List<string>)ViewState["LstCountCN"];
        LstParticularCN = (List<string>)ViewState["LstParticularCN"];
        LstServiceCN = (List<string>)ViewState["LstServiceCN"];
        LstTDSAmtCN = (List<string>)ViewState["LstTDSAmtCN"];
        LstTDSAmtWithoutSTCN = (List<string>)ViewState["LstTDSAmtWithoutSTCN"];
        LstDicountCN = (List<string>)ViewState["LstDicountCN"];
        LstDicountAmtCN = (List<string>)ViewState["LstDicountAmtCN"];
        LstWriteOffCN = (List<string>)ViewState["LstWriteOffCN"];
        LstWriteOffAmtCN = (List<string>)ViewState["LstWriteOffAmtCN"];
        LstServiceTaxAmtCN = (List<string>)ViewState["LstServiceTaxAmtCN"];
        LstPECessAmtCN = (List<string>)ViewState["LstPECessAmtCN"];
        LstHECessAmtCN = (List<string>)ViewState["LstHECessAmtCN"];
        LstTotalAmtCN = (List<string>)ViewState["LstTotalAmtCN"];
        if (LstCountCN != null)
        {
            int Count = Convert.ToInt32(LstCountCN.Count);
            for (int i = 0; i < Count; i++)
            {


                HtmlTableRow HTRow = new HtmlTableRow();

                HtmlTableCell HTCell1 = new HtmlTableCell();
                TextBox txtParticularCN = new TextBox();
                txtParticularCN.ID = LstParticularCN[i].ToString();
                txtParticularCN.CssClass = "DisableInput";
                txtParticularCN.Width = Unit.Percentage(98);
                txtParticularCN.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HTCell1.Controls.Add(txtParticularCN);

                HtmlTableCell HTCell2 = new HtmlTableCell();
                TextBox txtServiceCN = new TextBox();
                txtServiceCN.ID = LstServiceCN[i].ToString();
                txtServiceCN.CssClass = "DisableInput";
                txtServiceCN.Width = Unit.Percentage(98);
                txtServiceCN.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HTCell2.Controls.Add(txtServiceCN);

                HtmlTableCell HTCell3 = new HtmlTableCell();
                TextBox txtTDSAmtCN = new TextBox();
                txtTDSAmtCN.ID = LstTDSAmtCN[i].ToString();
                txtTDSAmtCN.CssClass = "inputnumber";
                txtTDSAmtCN.Width = Unit.Percentage(98);
                txtTDSAmtCN.Attributes.Add("onkeypress", "return isNumberKey(this.id, event);");
                txtTDSAmtCN.Attributes.Add("onblur", "return CalculateTdsCreditNoteAmt(this.id);");
                HiddenField hidTDSAmtWithoutSTCN = new HiddenField();
                hidTDSAmtWithoutSTCN.ID = LstTDSAmtWithoutSTCN[i].ToString();
                HiddenField hidServiceTaxAmtCN = new HiddenField();
                hidServiceTaxAmtCN.ID = LstServiceTaxAmtCN[i].ToString();
                HiddenField hidPECessAmtCN = new HiddenField();
                hidPECessAmtCN.ID = LstPECessAmtCN[i].ToString();
                HiddenField hidHECessAmtCN = new HiddenField();
                hidHECessAmtCN.ID = LstHECessAmtCN[i].ToString();
                HTCell3.Controls.Add(txtTDSAmtCN);
                HTCell3.Controls.Add(hidTDSAmtWithoutSTCN);
                HTCell3.Controls.Add(hidServiceTaxAmtCN);
                HTCell3.Controls.Add(hidPECessAmtCN);
                HTCell3.Controls.Add(hidHECessAmtCN);

                HtmlTableCell HTCell4 = new HtmlTableCell();
                TextBox txtDiscountCN = new TextBox();
                txtDiscountCN.ID = LstDicountCN[i].ToString();
                txtDiscountCN.CssClass = "inputnumber";
                txtDiscountCN.Width = Unit.Percentage(98);
                txtDiscountCN.Attributes.Add("onkeypress", "return isNumberKey(this.id, event);");
                txtDiscountCN.Attributes.Add("onblur", "return CalculateTdsCreditNoteAmt(this.id);");
                HiddenField hidDiscountAmtCN = new HiddenField();
                hidDiscountAmtCN.ID = LstDicountAmtCN[i].ToString();
                HTCell4.Controls.Add(txtDiscountCN);
                HTCell4.Controls.Add(hidDiscountAmtCN);


                HtmlTableCell HTCell5 = new HtmlTableCell();
                TextBox txtWriteOffCN = new TextBox();
                txtWriteOffCN.ID = LstWriteOffCN[i].ToString();
                txtWriteOffCN.CssClass = "inputnumber";
                txtWriteOffCN.Width = Unit.Percentage(98);
                txtWriteOffCN.Attributes.Add("onkeypress", "return isNumberKey(this.id, event);");
                txtWriteOffCN.Attributes.Add("onblur", "return CalculateTdsCreditNoteAmt(this.id);");
                HiddenField hidWriteOffAmtCN = new HiddenField();
                hidWriteOffAmtCN.ID = LstWriteOffAmtCN[i].ToString();
                HTCell5.Controls.Add(txtWriteOffCN);
                HTCell5.Controls.Add(hidWriteOffAmtCN);


                HtmlTableCell HTCell6 = new HtmlTableCell();
                TextBox txtTotalAmtCN = new TextBox();
                txtTotalAmtCN.ID = LstTotalAmtCN[i].ToString();
                txtTotalAmtCN.CssClass = "DisableInputNumber";
                txtTotalAmtCN.Width = Unit.Percentage(98);
                txtTotalAmtCN.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HTCell6.Controls.Add(txtTotalAmtCN);


                HTRow.Cells.Add(HTCell1);
                HTRow.Cells.Add(HTCell2);
                HTRow.Cells.Add(HTCell3);
                HTRow.Cells.Add(HTCell4);
                HTRow.Cells.Add(HTCell5);
                HTRow.Cells.Add(HTCell6);
                tblTdsCreditNote.Rows.Add(HTRow);
            }
        }
    }
    private void _AddRow_tblTDSSaleReturn(int RowCount)
    {
        if (tblTDSSaleReturn.Rows.Count == RowCount)
        {
            HtmlTableRow HTRow = new HtmlTableRow();

            HtmlTableCell HTCell1 = new HtmlTableCell();
            TextBox txtParticularSR = new TextBox();
            txtParticularSR.ID = "txtParticularSR" + RowCount.ToString();
            txtParticularSR.CssClass = "DisableInput";
            txtParticularSR.Width = Unit.Percentage(98);
            txtParticularSR.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
            HTCell1.Controls.Add(txtParticularSR);
            LstCountSR.Add(RowCount.ToString());
            LstParticularSR.Add(txtParticularSR.ID);

            HtmlTableCell HTCell2 = new HtmlTableCell();
            TextBox txtServiceSR = new TextBox();
            txtServiceSR.ID = "txtServiceSR" + RowCount.ToString();
            txtServiceSR.CssClass = "DisableInput";
            txtServiceSR.Width = Unit.Percentage(98);
            txtServiceSR.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
            HTCell2.Controls.Add(txtServiceSR);
            LstServiceSR.Add(txtServiceSR.ID);

            HtmlTableCell HTCell3 = new HtmlTableCell();
            TextBox txtSaleReturnSR = new TextBox();
            txtSaleReturnSR.ID = "txtSaleReturnSR" + RowCount.ToString();
            txtSaleReturnSR.CssClass = "DisableInputNumber";
            txtSaleReturnSR.Width = Unit.Percentage(98);
            txtSaleReturnSR.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
            HiddenField hidSaleReturnAmtSR = new HiddenField();
            hidSaleReturnAmtSR.ID = "hidSaleReturnAmtSR" + RowCount.ToString();
            HTCell3.Controls.Add(txtSaleReturnSR);
            HTCell3.Controls.Add(hidSaleReturnAmtSR);
            LstSaleReturnSR.Add(txtSaleReturnSR.ID);
            LstSaleReturnAmtSR.Add(hidSaleReturnAmtSR.ID);

            HtmlTableCell HTCell4 = new HtmlTableCell();
            TextBox txtTotalAmtSR = new TextBox();
            txtTotalAmtSR.ID = "txtTotalAmtSR" + RowCount.ToString();
            txtTotalAmtSR.CssClass = "DisableInputNumber";
            txtTotalAmtSR.Width = Unit.Percentage(98);
            txtTotalAmtSR.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
            HTCell4.Controls.Add(txtTotalAmtSR);
            LstTotalAmtSR.Add(txtTotalAmtSR.ID);


            HTRow.Cells.Add(HTCell1);
            HTRow.Cells.Add(HTCell2);
            HTRow.Cells.Add(HTCell3);
            HTRow.Cells.Add(HTCell4);
            tblTDSSaleReturn.Rows.Add(HTRow);

            hidRowCountSR.Value = RowCount.ToString();


            ViewState["LstCountSR"] = LstCountSR;
            ViewState["LstParticularSR"] = LstParticularSR;
            ViewState["LstServiceSR"] = LstServiceSR;
            ViewState["LstSaleReturnSR"] = LstSaleReturnSR;
            ViewState["LstSaleReturnAmtSR"] = LstSaleReturnAmtSR;
            ViewState["LstTotalAmtSR"] = LstTotalAmtSR;
        }
    }
    private void _Override_tblTDSSaleReturn()
    {
        LstCountSR = (List<string>)ViewState["LstCountSR"];
        LstParticularSR = (List<string>)ViewState["LstParticularSR"];
        LstServiceSR = (List<string>)ViewState["LstServiceSR"];
        LstSaleReturnSR = (List<string>)ViewState["LstSaleReturnSR"];
        LstSaleReturnAmtSR = (List<string>)ViewState["LstSaleReturnAmtSR"];
        LstTotalAmtSR = (List<string>)ViewState["LstTotalAmtSR"];
        if (LstCountSR != null)
        {
            int Count = Convert.ToInt32(LstCountSR.Count);
            for (int i = 0; i < Count; i++)
            {
                HtmlTableRow HTRow = new HtmlTableRow();

                HtmlTableCell HTCell1 = new HtmlTableCell();
                TextBox txtParticularSR = new TextBox();
                txtParticularSR.ID = LstParticularSR[i].ToString();
                txtParticularSR.CssClass = "DisableInput";
                txtParticularSR.Width = Unit.Percentage(98);
                txtParticularSR.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HTCell1.Controls.Add(txtParticularSR);

                HtmlTableCell HTCell2 = new HtmlTableCell();
                TextBox txtServiceSR = new TextBox();
                txtServiceSR.ID = LstServiceSR[i].ToString();
                txtServiceSR.CssClass = "DisableInput";
                txtServiceSR.Width = Unit.Percentage(98);
                txtServiceSR.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HTCell2.Controls.Add(txtServiceSR);

                HtmlTableCell HTCell3 = new HtmlTableCell();
                TextBox txtSaleReturnSR = new TextBox();
                txtSaleReturnSR.ID = LstSaleReturnSR[i].ToString();
                txtSaleReturnSR.CssClass = "DisableInputNumber";
                txtSaleReturnSR.Width = Unit.Percentage(98);
                txtSaleReturnSR.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HiddenField hidSaleReturnAmtSR = new HiddenField();
                hidSaleReturnAmtSR.ID = LstSaleReturnAmtSR[i].ToString();
                HTCell3.Controls.Add(txtSaleReturnSR);
                HTCell3.Controls.Add(hidSaleReturnAmtSR);

                HtmlTableCell HTCell4 = new HtmlTableCell();
                TextBox txtTotalAmtSR = new TextBox();
                txtTotalAmtSR.ID = LstTotalAmtSR[i].ToString();
                txtTotalAmtSR.CssClass = "DisableInputNumber";
                txtTotalAmtSR.Width = Unit.Percentage(98);
                txtTotalAmtSR.Attributes.Add("onkeydown", "return EnabledTabKey(this.id, event);");
                HTCell4.Controls.Add(txtTotalAmtSR);

                HTRow.Cells.Add(HTCell1);
                HTRow.Cells.Add(HTCell2);
                HTRow.Cells.Add(HTCell3);
                HTRow.Cells.Add(HTCell4);
                tblTDSSaleReturn.Rows.Add(HTRow);
            }
        }
    }
    private void _ResetTables()
    {
        _DeleteAllRow_MainTable();
        _DeleteAllRow_Details();
        _GetParticular_MainTable(ddlInvoiceParticular1);
        txtAddDescription1.Text = "";
        txtOriginalAmt1.Text = "";
        txtPendingAmt1.Text = "";
        txtTotalAmount1.Text = "";
        ddlServices1.Items.Clear();
        txtTaxableAmt.Text = "";
        txtNonTaxableAmt.Text = "";
        txtPECessAmt.Text = "";
        txtHECessAmt.Text = "";
        txtServiceTaxAmt.Text = "";
        txtTotalAmount.Text = "";
    }
    private string _GetRembursmentService(string strServiceId)
    {
        string strTaxability = "1";
        strsql = "Select TAXABILITY From IVC_ServiceMaster where SERVICEID='" + strServiceId + "'";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            if (DT.Rows[0]["TAXABILITY"].ToString() == "2")
            {

                strTaxability = "0";
            }
            else
            {
                strTaxability = "1";
            }
        }
        return strTaxability;
    }
    private void _DeleteZeroValueItemInDropdownlist(DropDownList ddl)
    {
        if (ddl.Items.Count > 0)
        {
            for (int i = 0; i < ddl.Items.Count; i++)
            {
                if (ddl.Items[i].Value == "0")
                {
                    ddl.Items.RemoveAt(i);
                    break;
                }
            }
        }
    }
    protected void ddlCustomer_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        _ResetTables();
        //display.DataSource = _ReturnParticular();
        //display.DataBind();
        //GridView1.DataSource = _ReturnPendingService_Details();
        //GridView1.DataBind();
    }
    protected void ddlFormat_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        string strFinyear = Session["FINYEAR"].ToString();
        if (strFinyear != string.Empty)
        {
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TFOID",ddlFormat.SelectedValue),
                                        new SqlParameter("@FINYEAR",strFinyear)
                                    };
            DataTable DTVoucher = DU.getdatausingprocedure("USP_IVC_GetMaxVoucherNo", sparam);
            if (DTVoucher.Rows.Count > 0)
            {
                txtPrefix.Text = DTVoucher.Rows[0]["prefix"].ToString();
                txtCreditNoteNo.Text = DTVoucher.Rows[0]["vouno"].ToString();
                txtSuffix.Text = DTVoucher.Rows[0]["suffix"].ToString();
            }
            else
            {
                txtPrefix.Text = "";
                txtCreditNoteNo.Text = "";
                txtSuffix.Text = "";
            }
        }
        _BindEditTranFormat_HeadOfAccounts(ddlFormat.SelectedValue);
        _EnableDisableControls(ddlFormat.SelectedValue);
    }
    private void _BindEditTranFormat_HeadOfAccounts(string strFormatId)
    {
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                       new SqlParameter("@FORMATID",strFormatId)
                                   };
            DataTable DTFormatHead = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat_HeadOfAccounts", sparam);
            if (DTFormatHead.Rows.Count > 0)
            {
                hidTDSLid.Value = DTFormatHead.Rows[0]["TDS_LEDGERID"].ToString();
                hidWriteOffLid.Value = DTFormatHead.Rows[0]["WRITEOFF_LEDGERID"].ToString();
            }
        }
    }
    protected void rbtCreditNoteType_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        _ResetTables();
        if (rbtCreditNoteType.SelectedValue == "0")
        {
            panelCreditNote.Visible = true;
            panelSalesReturn.Visible = false;
        }
        else if (rbtCreditNoteType.SelectedValue == "1")
        {
            panelCreditNote.Visible = false;
            panelSalesReturn.Visible = true;
        }

    }
    protected void txtDate_OnTextChanged(object sender, EventArgs e)
    {
        _GetCurrentServiceTaxRate(txtDate.Text.ToString());
    }
    protected void btnAddMore_CLick(object sender, EventArgs e)
    {
        _AddMoreRow_MainTable();
    }
    protected void btnDeleteRow_Click(object sender, EventArgs e)
    {
        _DeleteAllRow_MainTable();
    }
    protected void ddlParticular_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        int rowIndex = Convert.ToInt32(ddl.ID.ToString().Substring(20));
        DataTable DTParticular = _ReturnParticular();
        DataTable DTService = _ReturnPendingService_Details();

        DropDownList ddlServices = (DropDownList)tblCreditNoteMain.Rows[rowIndex].FindControl("ddlServices" + rowIndex);
        TextBox txtOriginalAmt = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtOriginalAmt" + rowIndex);
        TextBox txtPendingAmt = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtPendingAmt" + rowIndex);
        TextBox txtTotalAmount = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtTotalAmount" + rowIndex);
        HiddenField hidServiceTaxRate = (HiddenField)tblCreditNoteMain.Rows[rowIndex].FindControl("hidServiceTaxRate" + rowIndex);
        HiddenField hidPECessRate = (HiddenField)tblCreditNoteMain.Rows[rowIndex].FindControl("hidPECessRate" + rowIndex);
        HiddenField hidHECessRate = (HiddenField)tblCreditNoteMain.Rows[rowIndex].FindControl("hidHECessRate" + rowIndex);

        _DeleteZeroValueItemInDropdownlist(ddl);

        if (ddl.SelectedValue != "0")
        {
            DataView DVParticular = DTParticular.DefaultView;
            DVParticular.RowFilter = "ReferenceId='" + ddl.SelectedValue + "'";
            DataTable DTTempParticular = DVParticular.ToTable();

            string[] strOriginal = DTTempParticular.Rows[0]["OriginalAmount1"].ToString().Split(' ');
            txtOriginalAmt.Text = string.Format("{0:0.00}", Convert.ToDouble(strOriginal[0])) + " " + strOriginal[1];

            string[] strPending = DTTempParticular.Rows[0]["PendingAmount1"].ToString().Split(' ');
            txtPendingAmt.Text = string.Format("{0:0.00}", Convert.ToDouble(strPending[0])) + " " + strPending[1];

            DataTable DTServiceTaxRate = _GetBilledServiceTaxRate(DTTempParticular.Rows[0]["TransactionID"].ToString());
            if (DTServiceTaxRate.Rows.Count > 0)
            {
                hidServiceTaxRate.Value = DTServiceTaxRate.Rows[0]["SERVICETAXRATE"].ToString();
                hidPECessRate.Value = DTServiceTaxRate.Rows[0]["EDUCESS1RATE"].ToString();
                hidHECessRate.Value = DTServiceTaxRate.Rows[0]["EDUCESS2RATE"].ToString();
            }
            else
            {
                hidServiceTaxRate.Value = "0";
                hidPECessRate.Value = "0";
                hidHECessRate.Value = "0";
            }
            txtTotalAmount.Text = "";
            DataView DVService = DTService.DefaultView;
            DVService.RowFilter = "ReferenceSerial='" + ddl.SelectedValue + "'";
            DataTable DTTempService = DVService.ToTable();
            if (DTTempService.Rows.Count > 0)
            {
                ddlServices.DataSource = DTTempService;
                ddlServices.DataTextField = "ServiceName";
                ddlServices.DataValueField = "ServiceId";
                ddlServices.DataBind();
                ddlServices.Items.Insert(0, new ListItem("", "0"));
            }
            else
            {
                ddlServices.Items.Clear();
                ddlServices.Items.Insert(0, new ListItem("", "0"));
            }
        }
        else
        {
            ddlServices.Items.Clear();
            ddlServices.Items.Insert(0, new ListItem("", "0"));
        }
    }
    protected void ddlServiecs_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        int rowIndex = Convert.ToInt32(ddl.ID.ToString().Substring(11));
        DataTable DTService = _ReturnPendingService_Details();
        DropDownList ddlInvoiceParticular = (DropDownList)tblCreditNoteMain.Rows[rowIndex].FindControl("ddlInvoiceParticular" + rowIndex);
        TextBox txtOriginalAmt = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtOriginalAmt" + rowIndex);
        TextBox txtPendingAmt = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtPendingAmt" + rowIndex);
        TextBox txtTotalAmount = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtTotalAmount" + rowIndex);
        HiddenField hidTotalAmout = (HiddenField)tblCreditNoteMain.Rows[rowIndex].FindControl("hidTotalAmout" + rowIndex);
        HiddenField hidServiceTaxability = (HiddenField)tblCreditNoteMain.Rows[rowIndex].FindControl("hidServiceTaxability" + rowIndex);
//_DeleteZeroValueItemInDropdownlist(ddl);
        if (ddl.SelectedValue != "0")
        {
            DataView DVService = new DataView();
            DVService = DTService.DefaultView;
            DVService.RowFilter = "ReferenceSerial='" + ddlInvoiceParticular.SelectedValue + "' and ServiceId='" + ddl.SelectedValue + "'";
            DataTable DTTempService = DVService.ToTable();
            txtOriginalAmt.Text = string.Format("{0:0.00}", Convert.ToDouble(DTTempService.Rows[0]["Amount"].ToString()));
            txtPendingAmt.Text = string.Format("{0:0.00}", Convert.ToDouble(DTTempService.Rows[0]["PendingAmount"].ToString()));
            txtTotalAmount.Text = string.Format("{0:0.00}", Convert.ToDouble(DTTempService.Rows[0]["PendingAmount"].ToString()));
            hidTotalAmout.Value = string.Format("{0:0.00}", Convert.ToDouble(DTTempService.Rows[0]["PendingAmount"].ToString()));
            hidServiceTaxability.Value = _GetRembursmentService(ddl.SelectedValue);
            if (rbtCreditNoteType.SelectedValue == "0")
            {
                _AddRow_tblCreditNote(rowIndex);
                _BindDetails_tblTDSCreditNote(rowIndex);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "CalculateTotalCreditNoteAmt();", true);
            }
            else if (rbtCreditNoteType.SelectedValue == "1")
            {
                _AddRow_tblTDSSaleReturn(rowIndex);
                _BindDetails_tblTDSSaleReturn(rowIndex);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "CalculateTotalSaleReturnAmt();", true);
            }
        }
        else
        {
            txtOriginalAmt.Text = "";
            txtPendingAmt.Text = "";
            txtTotalAmount.Text = "";
            hidServiceTaxability.Value = "1";
        }
       // ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "", true);
    }
    private string _GetServiceLedgerId(string strServiceId)
    {
        string strLedgerId = string.Empty;
        if (strServiceId != string.Empty)
        {
            strsql = "SELECT LEDGERID FROM IVC_SERVICEMASTER WHERE SERVICEID='" + strServiceId + "'";
            DataTable DTLedger = DU.getdata(strsql);
            if (DTLedger.Rows.Count > 0)
            {
                strLedgerId = DTLedger.Rows[0]["LEDGERID"].ToString();
            }
        }
        return strLedgerId;
    }
    private void _ResetControl(Control ctrl)
    {
        foreach (Control c in ctrl.Controls)
        {
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)(c)).Text = string.Empty;
            }
            if (c.GetType() == typeof(DropDownList))
            {
                ((DropDownList)(c)).SelectedValue = "0";
            }
            if (c.HasControls())
            {
                _ResetControl(c);
            }
        }
    }
    
    private void _EnableDisableControls(string strFormatId)
    { 
        if (strFormatId != string.Empty)
        {
            SqlParameter[] sparam ={
                                   new  SqlParameter("@FORMATID",strFormatId)
                               };
            DataTable DTFormat = DU.getdatausingprocedure("USP_IVC_GetTransactionFormat", sparam);
            if (DTFormat.Rows.Count > 0)
            {
                if (Convert.ToBoolean(DTFormat.Rows[0]["NARRATION"]) == true)
                {
                    txtNarration.Text = "";
                    txtNarration.Enabled = true;
                }
                else
                {
                    txtNarration.Text = "";
                    txtNarration.Enabled = false;
                }
                if (Convert.ToBoolean(DTFormat.Rows[0]["REMARKS"]) == true)
                {
                    txtRemarks.Text = "";
                    txtRemarks.Enabled = true;
                }
                else
                {
                    txtRemarks.Text = "";
                    txtRemarks.Enabled = false;
                }
                if (Convert.ToBoolean(DTFormat.Rows[0]["PREPAREDBY"]) == true)
                {
                    txtPreparedBy.Text = "";
                    txtPreparedBy.Enabled = true;
                }
                else
                {
                    txtPreparedBy.Text = "";
                    txtPreparedBy.Enabled = false;
                }
                if (Convert.ToBoolean(DTFormat.Rows[0]["CHECKEDBY"]) == true)
                {
                    txtCheckedBy.Text = "";
                    txtCheckedBy.Enabled = true;
                }
                else
                {
                    txtCheckedBy.Text = "";
                    txtCheckedBy.Enabled = false;
                }
            }
        }
    }
    private string _SaveVouchers()
    {
        string strTranId = string.Empty;
        try
        {
            string strTranDate = string.Empty;
            string strTranTypeId = string.Empty;
            if (txtDate.Text != string.Empty)
            {
                string[] strTemp = txtDate.Text.Split('/');
                strTranDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];
            }
            SqlParameter[] sparam ={
                                    new SqlParameter("@PARENTTYPEID","2"),
                                    new SqlParameter("@BILLFORMATID",ddlFormat.SelectedValue),
                                    new SqlParameter("@MODULETYPE","3")
                                };
            DataTable DTTranTypeId = DU.getdatausingprocedure("USP_ACC_GetTransactionSettingBasedOnFormat", sparam);
            if (DTTranTypeId.Rows.Count > 0)
            {
                strTranTypeId = DTTranTypeId.Rows[0]["TypeID"].ToString();
            }
            if (strTranTypeId != string.Empty)
            {
                SqlParameter[] sparam1 = 
                                {
                                    new SqlParameter("@TransactionTypeID",strTranTypeId),      
                                    new SqlParameter("@TransactionNumber",txtCreditNoteNo.Text),      
                                    new SqlParameter("@FinYear",Session["FINYEAR"].ToString()),      
                                    new SqlParameter("@ReferenceNumber",txtRefNo.Text),     
                                    new SqlParameter("@IsCashVoucher","0"),      
                                    new SqlParameter("@TransactionDate",strTranDate),      
                                    new SqlParameter("@Narration",txtNarration.Text),     
                                    new SqlParameter("@CreditDays","0"),         
                                    new SqlParameter("@CreatedBy",Session["userid"].ToString()),      
                                    new SqlParameter("@IsCancelled","0"),      
                                    new SqlParameter("@CostCentreClassID","0")  
                                };
                int Result = DU.executeprocedure("USP_ACC_SaveTransaction", sparam1);
                if (Result > 0)
                {
                    strsql = "Select Ident_Current('ACC_VOUCHERS') as TranId";
                    DataTable DTTranId = DU.getdata(strsql);
                    if (DTTranId.Rows.Count > 0)
                    {
                        strTranId = DTTranId.Rows[0]["TranId"].ToString();
                        _SaveVouchers_Details(DTTranId.Rows[0]["TranId"].ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return strTranId;
    }
    private void _SaveVouchers_Details(string strTranId)
    {
        try
        {
            if (strTranId != string.Empty)
            {
                string strTranDate = string.Empty;
                if (txtDate.Text != string.Empty)
                {
                    string[] strTemp = txtDate.Text.Split('/');
                    strTranDate = strTemp[1] + "/" + strTemp[0] + "/" + strTemp[2];
                }
                if (ddlCustomer.SelectedValue != "0")
                {
                        string strCreditNoteId = _SaveCreditNote_Main(strTranId);
                        SqlParameter[] sparam =
                                                {
                                                    new SqlParameter("@TransactionId",strTranId),
                                                    new SqlParameter("@LedgerId",ddlCustomer.SelectedValue),
                                                    new SqlParameter("@Amount",txtTotalAmount.Text),
                                                    new SqlParameter("@IsReconciled","0"),
                                                    new SqlParameter("@ReconciledDate",strTranDate)
                                                };
                        int Result = DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                        if (Result > 0)
                        {
                            strsql = "Select Ident_Current('ACC_VOUDETAILS') as VouSerial";
                            DataTable DT = DU.getdata(strsql);
                            if (DT.Rows.Count > 0)
                            {
                                _SaveReference_Details(DT.Rows[0]["VouSerial"].ToString(), strCreditNoteId);
                            }
                        }
                   }
                if (rbtCreditNoteType.SelectedValue == "0")
                {
                    int count = Convert.ToInt32(hidRowCountCN.Value);
                    double tdsAmount = 0;
                    double WriteOffAmount = 0;
                    for (int i = 1; i <= count; i++)
                    {
                        DropDownList ddlServices = (DropDownList)tblCreditNoteMain.Rows[i].FindControl("ddlServices" + i.ToString());
                        TextBox txtTDSAmtCN = (TextBox)tblTdsCreditNote.Rows[i].FindControl("txtTDSAmtCN" + i.ToString());
                        HiddenField hidDiscountAmtCN = (HiddenField)tblTdsCreditNote.Rows[i].FindControl("hidDiscountAmtCN" + i.ToString());
                        HiddenField hidWriteOffAmtCN = (HiddenField)tblTdsCreditNote.Rows[i].FindControl("hidWriteOffAmtCN" + i.ToString());
                        if (ddlServices.SelectedValue != "0")
                        {
                            string strServiceLid = _GetServiceLedgerId(ddlServices.SelectedValue);
                            if (hidDiscountAmtCN.Value != string.Empty && Convert.ToDouble(hidDiscountAmtCN.Value) > 0 && strServiceLid != string.Empty)
                            {
                                double amount = 0 - Convert.ToDouble(hidDiscountAmtCN.Value);
                                SqlParameter[] sparam =
                                            {
                                                new SqlParameter("@TransactionId",strTranId),
                                                new SqlParameter("@LedgerId",strServiceLid),
                                                new SqlParameter("@Amount",amount),
                                                new SqlParameter("@IsReconciled","0"),
                                                new SqlParameter("@ReconciledDate",strTranDate)
                                            };
                                DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                            }
                        }
                        if (txtTDSAmtCN.Text != string.Empty && Convert.ToDouble(txtTDSAmtCN.Text.Trim()) > 0)
                        {
                            tdsAmount += Convert.ToDouble(txtTDSAmtCN.Text.Trim());
                        }
                        if (hidWriteOffAmtCN.Value != string.Empty && Convert.ToDouble(hidWriteOffAmtCN.Value) > 0)
                        {
                            WriteOffAmount += Convert.ToDouble(hidWriteOffAmtCN.Value);
                        }
                    }
                    if (tdsAmount > 0)
                    {
                        double amount = 0 - tdsAmount;
                        SqlParameter[] sparam =
                                            {
                                                new SqlParameter("@TransactionId",strTranId),
                                                new SqlParameter("@LedgerId",hidTDSLid.Value),
                                                new SqlParameter("@Amount",amount),
                                                new SqlParameter("@IsReconciled","0"),
                                                new SqlParameter("@ReconciledDate",strTranDate)
                                            };
                        DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                    }
                    if (WriteOffAmount > 0)
                    {
                        double amount = 0 - WriteOffAmount;
                        SqlParameter[] sparam =
                                            {
                                                new SqlParameter("@TransactionId",strTranId),
                                                new SqlParameter("@LedgerId",hidWriteOffLid.Value),
                                                new SqlParameter("@Amount",amount),
                                                new SqlParameter("@IsReconciled","0"),
                                                new SqlParameter("@ReconciledDate",strTranDate)
                                            };
                        DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                    }
                }
                else if (rbtCreditNoteType.SelectedValue == "1")
                {
                    int count = Convert.ToInt32(hidRowCountSR.Value);
                    for (int i = 1; i <= count; i++)
                    {
                        DropDownList ddlServices = (DropDownList)tblCreditNoteMain.Rows[i].FindControl("ddlServices" + i.ToString());
                        TextBox txtTotalAmtSR = (TextBox)tblTDSSaleReturn.Rows[i].FindControl("txtTotalAmtSR" + i.ToString());
                        if (ddlServices.SelectedValue != "0")
                        {
                            string strServiceLid = _GetServiceLedgerId(ddlServices.SelectedValue);
                            if (txtTotalAmtSR.Text != string.Empty && Convert.ToDouble(txtTotalAmtSR.Text) > 0 && strServiceLid != string.Empty)
                            {
                                double amount = 0 - Convert.ToDouble(txtTotalAmtSR.Text.Trim());
                                SqlParameter[] sparam =
                                            {
                                                new SqlParameter("@TransactionId",strTranId),
                                                new SqlParameter("@LedgerId",strServiceLid),
                                                new SqlParameter("@Amount",amount),
                                                new SqlParameter("@IsReconciled","0"),
                                                new SqlParameter("@ReconciledDate",strTranDate)
                                            };
                                DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                            }
                        }
                    }
                }
                if (txtServiceTaxAmt.Text != string.Empty && Convert.ToDouble(txtServiceTaxAmt.Text) > 0 && hidServiceTaxLid.Value != string.Empty)
                {
                    double amount = 0 - Convert.ToDouble(txtServiceTaxAmt.Text.Trim());
                    SqlParameter[] sparam =
                                            {
                                                new SqlParameter("@TransactionId",strTranId),
                                                new SqlParameter("@LedgerId",hidServiceTaxLid.Value),
                                                new SqlParameter("@Amount",amount),
                                                new SqlParameter("@IsReconciled","0"),
                                                new SqlParameter("@ReconciledDate",strTranDate)
                                            };
                    DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);

                }
                if (txtPECessAmt.Text != string.Empty && Convert.ToDouble(txtPECessAmt.Text) > 0 && hidPECessLid.Value != string.Empty)
                {
                    double amount = 0 - Convert.ToDouble(txtPECessAmt.Text.Trim());
                    SqlParameter[] sparam =
                                            {
                                                new SqlParameter("@TransactionId",strTranId),
                                                new SqlParameter("@LedgerId",hidPECessLid.Value),
                                                new SqlParameter("@Amount",amount),
                                                new SqlParameter("@IsReconciled","0"),
                                                new SqlParameter("@ReconciledDate",strTranDate)
                                            };
                    DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);

                }
                if (txtHECessAmt.Text != string.Empty && Convert.ToDouble(txtHECessAmt.Text) > 0 && hidHECessLid.Value != string.Empty)
                {
                    double amount = 0 - Convert.ToDouble(txtHECessAmt.Text.Trim());
                    SqlParameter[] sparam =
                                                {
                                                    new SqlParameter("@TransactionId",strTranId),
                                                    new SqlParameter("@LedgerId",hidHECessLid.Value),
                                                    new SqlParameter("@Amount",amount),
                                                    new SqlParameter("@IsReconciled","0"),
                                                    new SqlParameter("@ReconciledDate",strTranDate)
                                                };
                    DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);

                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _SaveReference_Details(string strVouSerial, string strCreditNoteId)
    {
        try
        {
            if (strVouSerial != string.Empty && strCreditNoteId != string.Empty)
            {
                string strRefDate = string.Empty;
                string strRefferenceNo = string.Empty;
                if (txtDate.Text.ToString() != string.Empty)
                {
                    string[] str = txtDate.Text.ToString().Split('/');
                    strRefDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
                }
                int rowCount = Convert.ToInt32(hidRowCountMain.Value);
                for (int i = 1; i <= rowCount; i++)
                {
                    DropDownList ddlInvoiceParticular = (DropDownList)tblCreditNoteMain.Rows[i].FindControl("ddlInvoiceParticular" + i);
                    TextBox txtCreditNoteAmt = (TextBox)tblCreditNoteMain.Rows[i].FindControl("txtTotalAmount" + i);
                    if (ddlInvoiceParticular.SelectedValue != "0" && txtCreditNoteAmt.Text != string.Empty)
                    {
                        SqlParameter[] sparam = {
                                            new SqlParameter("@ReferenceSerial","0"),
                                            new SqlParameter("@ParentReference",ddlInvoiceParticular.SelectedValue),
                                            new SqlParameter("@TransactionSerial",strVouSerial),
                                            new SqlParameter("@ReferenceType","2"),
                                            new SqlParameter("@Reference",ddlInvoiceParticular.SelectedItem.Text),
                                            new SqlParameter("@ReferenceDate",strRefDate),
                                            new SqlParameter("@CreditDays","0"),
                                            new SqlParameter("@ReferenceAmount",txtTotalAmount.Text.Trim()),
                                            new SqlParameter("@IsTDSDeducted","0")
                                };
                        int result = DU.executeprocedure("USP_ACC_SaveReferenceDetails", sparam);
                        if (result > 0)
                        {
                            strsql = "Select Ident_Current('ACC_REFDETAILS') as REFSERIAL";
                            DataTable DTCrediteNote = DU.getdata(strsql);
                            if (DTCrediteNote.Rows.Count > 0)
                            {
                                _SaveCreditNote_Child(strCreditNoteId, DTCrediteNote.Rows[0]["REFSERIAL"].ToString(), i);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private string _SaveCreditNote_Main(string strTranId)
    {
        string strCreditNoteId = string.Empty;
        try
        {
            if (strTranId != string.Empty)
            {
                SqlParameter[] sparam ={
                                       new SqlParameter("@CREDITNOTEID","0"),
                                       new SqlParameter("@TRANID",strTranId),
                                       new SqlParameter("@FORMATID",ddlFormat.SelectedValue),
                                       new SqlParameter("@CUSTOMERLEDGERID",ddlCustomer.SelectedValue),
                                       new SqlParameter("@REMARKS",txtRemarks.Text.Trim()),
                                       new SqlParameter("@CHECKEDBY",txtCheckedBy.Text.Trim()),
                                       new SqlParameter("@PREPAREDBY",txtPreparedBy.Text.Trim()),
                                       new SqlParameter("@ISTDSAPPLIED","0"),
                                       new SqlParameter("@ISSALESRETURN",rbtCreditNoteType.SelectedValue)
                                   };
                int result = DU.executeprocedure("USP_IVC_SaveCreditNote", sparam);
                if (result > 0)
                {
                    strsql = "Select Ident_Current('IVC_CREDITNOTE_MAIN') as CREDITNOTEID";
                    DataTable DTCrediteNote = DU.getdata(strsql);
                    if (DTCrediteNote.Rows.Count > 0)
                    {
                        strCreditNoteId = DTCrediteNote.Rows[0]["CREDITNOTEID"].ToString();
                        _SaveCreditNote_Total(strCreditNoteId);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return strCreditNoteId;
    }
    private void _SaveCreditNote_Child(string strCreditNoteId, string strRefId, int rowIndex)
    {
        try
        {
            if (strCreditNoteId != string.Empty && strRefId != string.Empty)
            {
                DataTable DTParticular = _ReturnParticular();
                DataTable DTService = _ReturnPendingService_Details();


                DropDownList ddlInvoiceParticular = (DropDownList)tblCreditNoteMain.Rows[rowIndex].FindControl("ddlInvoiceParticular" + rowIndex);
                DropDownList ddlServices = (DropDownList)tblCreditNoteMain.Rows[rowIndex].FindControl("ddlServices" + rowIndex);
                TextBox txtTotalAmount = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtTotalAmount" + rowIndex);
                TextBox txtAddDescription = (TextBox)tblCreditNoteMain.Rows[rowIndex].FindControl("txtAddDescription" + rowIndex);

                DataView DVService = new DataView();
                DVService = DTService.DefaultView;
                DVService.RowFilter = "ReferenceSerial='" + ddlInvoiceParticular.SelectedValue + "' and ServiceId='" + ddlServices.SelectedValue + "'";
                DataTable DTTempService = DVService.ToTable();

                if (rbtCreditNoteType.SelectedValue == "0")
                {
                    TextBox txtTDSAmtCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtTDSAmtCN" + rowIndex);
                    TextBox txtDiscountCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtDiscountCN" + rowIndex);
                    TextBox txtWriteOffCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtWriteOffCN" + rowIndex);
                    HiddenField hidDiscountAmtCN = (HiddenField)tblTdsCreditNote.Rows[rowIndex].FindControl("hidDiscountAmtCN" + rowIndex);
                    HiddenField hidWriteOffAmtCN = (HiddenField)tblTdsCreditNote.Rows[rowIndex].FindControl("hidWriteOffAmtCN" + rowIndex);
                    if (txtTDSAmtCN.Text == string.Empty)
                    {
                        txtTDSAmtCN.Text = "0";
                    }
                    if (txtDiscountCN.Text == string.Empty)
                    {
                        txtDiscountCN.Text = "0";
                    }
                    if (txtWriteOffCN.Text == string.Empty)
                    {
                        txtWriteOffCN.Text = "0";
                    }
                    if (hidDiscountAmtCN.Value == string.Empty)
                    {
                        hidDiscountAmtCN.Value = "0";
                    }
                    if (hidWriteOffAmtCN.Value == string.Empty)
                    {
                        hidWriteOffAmtCN.Value = "0";
                    }

                    if (DTTempService.Rows.Count > 0)
                    {
                        SqlParameter[] sparam ={
                                       new SqlParameter("@CREDITNOTECHILDID","0"),
                                       new SqlParameter("@CREDITNOTEID",strCreditNoteId),
                                       new SqlParameter("@REFERENCETRANID",DTTempService.Rows[0]["TRANID"].ToString()),
                                       new SqlParameter("@REFERENCECHILDID",DTTempService.Rows[0]["CHILDID"].ToString()),
                                       new SqlParameter("@REFERENCEID",strRefId),
                                       new SqlParameter("@SERVICEID",ddlServices.SelectedValue),
                                       new SqlParameter("@ADDDESC",txtAddDescription.Text.Trim()),
                                       new SqlParameter("@TDSAMOUNT",txtTDSAmtCN.Text),
                                       new SqlParameter("@WRITEOFFAMT",txtWriteOffCN.Text.Trim()),
                                       new SqlParameter("@WRITEOFFTAXABLEAMT",hidWriteOffAmtCN.Value),
                                       //new SqlParameter("@WRITEOFFABATEMENTAMT",""),
                                       //new SqlParameter("@WRITEOFFSERVICECHARGEAMT",""),
                                       new SqlParameter("@DISCOUNTAMT",txtDiscountCN.Text.Trim()),
                                       new SqlParameter("@DISCOUNTTAXABLEAMT",hidDiscountAmtCN.Value),
                                       //new SqlParameter("@DISCOUNTABATEMENTAMT",txtCheckedBy.Text.Trim()),
                                       //new SqlParameter("@DISCOUNTSERVICECHARGEAMT",txtPreparedBy.Text.Trim()),
                                       new SqlParameter("@AMOUNT",txtTotalAmount.Text)
                                   };
                        int result = DU.executeprocedure("USP_IVC_SaveCreditNoteChildDetails", sparam);
                        if (result > 0)
                        {
                            strsql = "Select Ident_Current('IVC_CREDITNOTE_CHILD') as CREDITNOTECHILDID";
                            DataTable DTCrediteNote = DU.getdata(strsql);
                            if (DTCrediteNote.Rows.Count > 0)
                            {
                                _SaveCreditNote_TDS(DTCrediteNote.Rows[0]["CREDITNOTECHILDID"].ToString(), rowIndex);
                            }
                        }
                    }
                }
                else if (rbtCreditNoteType.SelectedValue == "1")
                {
                    HiddenField hidSaleReturnAmtSR = (HiddenField)tblTDSSaleReturn.Rows[rowIndex].FindControl("hidSaleReturnAmtSR" + rowIndex);
                    if (hidSaleReturnAmtSR.Value == string.Empty)
                    {
                        hidSaleReturnAmtSR.Value = "0";
                    }
                    if (DTTempService.Rows.Count > 0)
                    {
                        SqlParameter[] sparam ={
                                       new SqlParameter("@CREDITNOTECHILDID","0"),
                                       new SqlParameter("@CREDITNOTEID",strCreditNoteId),
                                       new SqlParameter("@REFERENCETRANID",DTTempService.Rows[0]["TRANID"].ToString()),
                                       new SqlParameter("@REFERENCECHILDID",DTTempService.Rows[0]["CHILDID"].ToString()),
                                       new SqlParameter("@REFERENCEID",strRefId),
                                       new SqlParameter("@SERVICEID",ddlServices.SelectedValue),
                                       new SqlParameter("@ADDDESC",txtAddDescription.Text.Trim()),
                                       new SqlParameter("@TDSAMOUNT","0"),
                                       new SqlParameter("@WRITEOFFAMT","0"),
                                       //new SqlParameter("@WRITEOFFTAXABLEAMT",hidWriteOffAmtCN.Value),
                                       //new SqlParameter("@WRITEOFFABATEMENTAMT",""),
                                       //new SqlParameter("@WRITEOFFSERVICECHARGEAMT",""),
                                       new SqlParameter("@DISCOUNTAMT",txtTotalAmount.Text),
                                       new SqlParameter("@DISCOUNTTAXABLEAMT",hidSaleReturnAmtSR.Value),
                                       //new SqlParameter("@DISCOUNTABATEMENTAMT",txtCheckedBy.Text.Trim()),
                                       //new SqlParameter("@DISCOUNTSERVICECHARGEAMT",txtPreparedBy.Text.Trim()),
                                       new SqlParameter("@AMOUNT",txtTotalAmount.Text)
                                   };
                        int result = DU.executeprocedure("USP_IVC_SaveCreditNoteChildDetails", sparam);

                        if (result > 0)
                        {
                            strsql = "Select Ident_Current('IVC_CREDITNOTE_CHILD') as CREDITNOTECHILDID";
                            DataTable DTCrediteNote = DU.getdata(strsql);
                            if (DTCrediteNote.Rows.Count > 0)
                            {
                                SqlParameter[] sparam1 ={
                                       new SqlParameter("@CREDITNOTECHILDID",DTCrediteNote.Rows[0]["CREDITNOTECHILDID"].ToString()),
                                       new SqlParameter("@CESS1RATE","0"),
                                       new SqlParameter("@CESS1AMT","0"),
                                       new SqlParameter("@CESS2RATE","0"),
                                       new SqlParameter("@CESS2AMT","0"),
                                       new SqlParameter("@SERVICERATE","0"),
                                       new SqlParameter("@SERVICEAMT","0"),
                                       new SqlParameter("@TDS","0"),
                                       new SqlParameter("@TDSTAXABLEAMT","0"),
                                       new SqlParameter("@ABATEMENTAMT","0"),
                                       new SqlParameter("@SERVICECHARGEAMT","0")
                                   };
                                DU.executeprocedure("USP_IVC_SaveCreditNoteTDSDetails", sparam1);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _SaveCreditNote_Total(string strCreditNoteId)
    {
        try
        {
            if (strCreditNoteId != string.Empty)
            {
                SqlParameter[] sparam ={
                                       new  SqlParameter("@CREDITNOTEID",strCreditNoteId),
                                       new  SqlParameter("@TAXABLEAMT",txtTaxableAmt.Text.Trim()),
                                       new  SqlParameter("@NONTAXABLEAMT",txtNonTaxableAmt.Text.Trim()),
                                       new  SqlParameter("@SERVICETAXRATE",hidServiceTaxRateCurrent.Value),
                                       new  SqlParameter("@SERVICETAXAMT",txtServiceTaxAmt.Text.Trim()),
                                       new  SqlParameter("@EDUCESS1RATE",hidPECessRateCurrent.Value),
                                       new  SqlParameter("@EDUCESS1AMT",txtPECessAmt.Text.Trim()),
                                       new  SqlParameter("@EDUCESS2RATE",hidHECessRateCurrent.Value),
                                       new  SqlParameter("@EDUCESS2AMT",txtHECessAmt.Text.Trim()),
                                       new  SqlParameter("@TOTAL",txtTotalAmount.Text.Trim()),
                                       new  SqlParameter("@TDSAMOUNT",hidTotalTdsAmount.Value),
                                       new  SqlParameter("@WRITEOFFAMT",hidTotalWriteOffAmount.Value),
                                       new  SqlParameter("@DISCOUNTAMT",hidTotalDiscountAmount.Value),
                                       new  SqlParameter("@ADDITIONALTAXAMT",hidTotalAdditionalAmount.Value)
                                   };
                int Result = DU.executeprocedure("USP_IVC_SaveCreditNoteTotals", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string str = string.Empty;
            using (TransactionScope scope = new TransactionScope())
            {
                str = _SaveVouchers();
                scope.Complete();
            }
            if (str != string.Empty)
            {
                lblMsg.Text = "Credit Note Created Successfully...";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
    private void _SaveCreditNote_TDS(string strCreditNoteChildId, int rowIndex)
    {
        try
        {
            if (strCreditNoteChildId != string.Empty)
            {
                HiddenField hidServiceTaxRate = (HiddenField)tblCreditNoteMain.Rows[rowIndex].FindControl("hidServiceTaxRate" + rowIndex);
                HiddenField hidPECessRate = (HiddenField)tblCreditNoteMain.Rows[rowIndex].FindControl("hidPECessRate" + rowIndex);
                HiddenField hidHECessRate = (HiddenField)tblCreditNoteMain.Rows[rowIndex].FindControl("hidHECessRate" + rowIndex);
                TextBox txtTDSAmtCN = (TextBox)tblTdsCreditNote.Rows[rowIndex].FindControl("txtTDSAmtCN" + rowIndex);
                HiddenField hidServiceTaxAmtCN = (HiddenField)tblTdsCreditNote.Rows[rowIndex].FindControl("hidServiceTaxAmtCN" + rowIndex);
                HiddenField hidPECessAmtCN = (HiddenField)tblTdsCreditNote.Rows[rowIndex].FindControl("hidPECessAmtCN" + rowIndex);
                HiddenField hidHECessAmtCN = (HiddenField)tblTdsCreditNote.Rows[rowIndex].FindControl("hidHECessAmtCN" + rowIndex);

                HiddenField hidTDSAmtWithoutSTCN = (HiddenField)tblTdsCreditNote.Rows[rowIndex].FindControl("hidTDSAmtWithoutSTCN" + rowIndex);

                if (hidServiceTaxAmtCN.Value != string.Empty && Convert.ToDouble(hidServiceTaxAmtCN.Value) == 0)
                {
                    hidServiceTaxRate.Value = "0";
                    hidPECessRate.Value = "0";
                    hidHECessRate.Value = "0";
                    hidPECessAmtCN.Value = "0";
                    hidHECessAmtCN.Value = "0";
                    hidTDSAmtWithoutSTCN.Value = "0";
                }
                if (txtTDSAmtCN.Text == string.Empty)
                {
                    txtTDSAmtCN.Text = "0";
                }

                SqlParameter[] sparam ={
                                       new SqlParameter("@CREDITNOTECHILDID",strCreditNoteChildId),
                                       new SqlParameter("@CESS1RATE",hidPECessRate.Value),
                                       new SqlParameter("@CESS1AMT",hidPECessAmtCN.Value),
                                       new SqlParameter("@CESS2RATE",hidHECessRate.Value),
                                       new SqlParameter("@CESS2AMT",hidHECessAmtCN.Value),
                                       new SqlParameter("@SERVICERATE",hidServiceTaxRate.Value),
                                       new SqlParameter("@SERVICEAMT",hidServiceTaxAmtCN.Value),
                                       new SqlParameter("@TDS",txtTDSAmtCN.Text),
                                       new SqlParameter("@TDSTAXABLEAMT",hidTDSAmtWithoutSTCN.Value),
                                       new SqlParameter("@ABATEMENTAMT","0"),
                                       new SqlParameter("@SERVICECHARGEAMT","0")
                                   };
                DU.executeprocedure("USP_IVC_SaveCreditNoteTDSDetails", sparam);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

}
